<?php

	/**
	 * @see Bike_Initial
	 */
	require_once 'Bike/Initial.php';
	
	/**
	 * @see Bike_Js
	 */
	require_once 'Bike/Js.php';
	
	/**
	 * @see Bike_Form_Renderer
	 */
	require_once 'Bike/Form/Renderer.php';
	
	/**
	 * @see Bike_Form_Renderer_ExtJs
	 */
	require_once 'Bike/Form/Renderer/ExtJs.php';
	
		
	abstract class Bike_Form_Renderer_ExtJs_Abstract extends Bike_Initial
	{
		protected $_element;
		protected $_extJs;

		static public function getJs()
		{
			return Bike_Js::getInstance();
		}

		public function getElement()
		{			return $this->_element;
		}

		public function setElement($Element)
		{			$this->_element = $Element;
			$this->initWithElement();
			return $this;
		}

		public function initWithElement()
		{
		}

		public function getRenderer($Element)
		{			return Bike_Form_Renderer::factory($Element, Bike_Form_Renderer_ExtJs::TYPE);
		}

		protected function getExtJsSettings()
		{
			return array();
		}
		
		protected function prepareExtJsSettings(array $extJsSettings)
		{
			return $extJsSettings;
		}
		
		protected function getPreparedExtJsSettings()
		{
			return $this->prepareExtJsSettings($this->getExtJsSettings());
		}

		public function setExtJs()
		{
			if($this->getElement()->getExtJs())
			{				$this->_extJs = $this->getElement()->getExtJs();
				$this->getElement()->unsetExtJs();
				return $this;
			}
			return $this->_setExtJs();
		}

		protected function _setExtJs()
		{
			$className = $this->getConstBikeJsExtClass();
			$parts = explode('.', $className);
			$last = sizeof($parts) - 1;
			$js = $this->getJs();
			foreach($parts as $n => $part)
			{
				$js = ($n == $last) ? $js->$part($this->getPreparedExtJsSettings()) : $js->$part;
			}
			$this->_extJs = $js;
			
			return $this;
		}
		
		public function afterSetExtJs()
		{
			return $this;
		}
		
		public function getExtJsObj()
		{
			if(!$this->getExtJs() instanceof Bike_Js_Interface_Js)
			{
				require_once 'Bike/Js/Exception.php';
				throw new Bike_Js_Exception('ExtJs setting not setted yet');
			}
			return $this->getExtJs()->_first();
		} 

		public function getExtJs()
		{			return $this->_extJs;
		}

		public function render(Zend_View_Interface $View = null)
		{
			return (string) $this->getExtJs();
		}
	}
